define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'booking/coupon/index' + location.search,
                    add_url: 'booking/coupon/add',
                    edit_url: 'booking/coupon/edit',
                    del_url: 'booking/coupon/del',
                    multi_url: 'booking/coupon/multi',
                    import_url: 'booking/coupon/import',
                    table: 'booking_coupon',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [{
                            checkbox: true
                        },
                        {
                            field: 'id',
                            title: __('Id')
                        },
                        {
                            field: 'name',
                            title: __('Name'),
                            operate: 'LIKE'
                        },                      
                        {
                            field: 'condition_name',
                            title: __('Condition')                          
                        },
                        {
                            field: 'result',
                            title: __('Result'),
                            visible:false,
                            searchList: {
                                "0": __('Result 0'),
                                "1": __('Result 1')
                            },
                            formatter: Table.api.formatter.normal
                        },
                        {
                            field: 'result_data',
                            title: __('Result'),
                            operate:false,
                            formatter: function(value,row){
                                if(row.result==0){
                                    return '订单满'+row.result_data.money+'元，打'+row.result_data.number+'折';
                                }else{
                                    return '订单满'+row.result_data.money+'元，减'+row.result_data.number+'元';
                                }
                            }
                        },
                        // {
                        //     field: 'result_data',
                        //     title: __('Result_data'),
                        //     operate: 'LIKE'
                        // },
                        {
                            field: 'give_num',
                            title: __('Give_num')
                        },
                        {
                            field: 'alone_num',
                            title: __('Alone_num')
                        },
                        {
                            field: 'received_num',
                            title: __('Received_num')
                        },
                        {
                            field: 'is_private',
                            title: __('Is_private'),
                            searchList: {
                                "yes": __('Is_private yes'),
                                "no": __('Is_private no')
                            },
                            formatter: Table.api.formatter.normal
                        },
                        {
                            field: 'is_open',
                            title: __('Is_open'),
                            searchList: {
                                "0": __('Is_open 0'),
                                "1": __('Is_open 1')
                            },
                            formatter: Table.api.formatter.normal
                        },
                        {
                            field: 'receive_times',
                            title: __('Receive_times'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter:function(value,row){
                                if(row.mode=='fixation'){
                                    return value+'天';
                                }else{
                                    return value;
                                }
                            }                         
                        },
                        {
                            field: 'use_times',
                            title: __('Use_times'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter:function(value,row){
                                if(row.mode=='fixation'){
                                    return value+'天';
                                }else{
                                    return value;
                                }
                            }                                        
                        },
                        {
                            field: 'url', title: __('Url'), formatter: function (value, row, index) {
                                return '<a href="'+Config.mobileurl+'#/pages/coupon/detail?id=' + row.id_text + '" target="_blank" class="btn btn-default btn-xs"><i class="fa fa-link"></i></a>';
                            }
                        },
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'updatetime',
                            title: __('Updatetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        result:function(){
            //优惠模式切换
            $('#c-result').on('change',function(){               
                if($(this).val()==0){
                    $('#text-a').html('打');
                    $('#text-b').html('折');
                }else{
                    $('#text-a').html('减');
                    $('#text-b').html('元');
                }
            })
            //去掉日期的选项
            $('#c-receive_times').data('ranges',1);
            $('#c-use_times').data('ranges',1);

            let receive_times1 = '';
            let receive_times2 = '';
            let use_times1 = '';
            let use_times2 = '';
            
            //有效期模式切换
            $('.modes').click(function(){
                var value = $(this).val()  //获取选中的radio的值
               
                if(value != 'fixation'){//固定日期
                    receive_times2 = $('#c-receive_times').val();
                    use_times2 = $('#c-use_times').val();

                    $('.receive_times').html(`<input id="c-receive_times" class="form-control datetimerange" autocomplete="off" data-use-current="true" name="row[receive_times]" type="text" value="${receive_times1}">`);
                    $('.use_times').html(`<input id="c-use_times" class="form-control datetimerange" autocomplete="off" data-use-current="true" name="row[use_times]" type="text" value="${use_times1}">`);
                    //去掉日期的选项
                    $('#c-receive_times').data('ranges',1);
                    $('#c-use_times').data('ranges',1);
                    Form.events.daterangepicker($("form[role=form]"));
                }else{//天数         
                    receive_times1 = $('#c-receive_times').val();
                    use_times1 = $('#c-use_times').val();

                    $('.receive_times').html(`
                    <div class="input-group">
                        <input id="c-receive_times" class="form-control" name="row[receive_times]" autocomplete="off" type="text" placeholder="表示未来X天内有效" value="${receive_times2}">
                        <div class="input-group-btn">
                            <button type="button" class="btn btn-success">天</button>
                        </div>
                    </div>`);

                    $('.use_times').html(`
                    <div class="input-group">
                        <input id="c-use_times" class="form-control" name="row[use_times]" type="text" autocomplete="off" placeholder="表示未来X天内有效" value="${use_times2}">
                        <div class="input-group-btn">
                            <button type="button" class="btn btn-success">天</button>
                        </div>
                    </div>`);
                }
            });
        },
        add: function () {
            this.result();
            Controller.api.bindevent();
        },
        edit: function () {
            this.result();
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});